package com.thy.controller;


import com.hospital.common.base.PageResult;
import com.hospital.common.base.Result;
import com.hospital.common.base.ThreadAttributes;
import com.hospital.common.dto.audit.module.BanDto;
import com.hospital.common.dto.audit.module.UnBanDto;
import com.hospital.common.dto.audit.module.UserDto;
import com.hospital.common.utils.ThreadLocalUtils;
import com.hospital.common.vo.audit.module.SectionVo;
import com.hospital.common.vo.audit.module.UserVo;
import com.thy.service.TBannedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 封禁列表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-26
 */
@RestController
@RequestMapping("/banned")
public class TBannedController {
    @Autowired
    private TBannedService tBannedService;

    /**
     * 获取封禁状态
     * @return Integer 1为封禁，0为正常
     */
    @GetMapping("/getBannedStatus")
    public Result<Integer> getBannedStatus(@RequestParam("sectionId") Long sectionId,@RequestParam(value = "userId",required = false)Long userId){
        if (userId == null){
            ThreadAttributes threadAttributes = ThreadLocalUtils.get();
            if (threadAttributes != null) {
                userId = threadAttributes.getUserId();
            } else {
                return Result.fail("未登录或会话已过期");
            }
        }
        Integer status  = tBannedService.getBannedStatus(userId,sectionId);
        return Result.success(status);
    }

    /**
     * 查询本板块的用户
     *
     * @param userDto
     * @return  PageResult<UserVo>
     */
   @PostMapping("/getUsers")
    public Result<PageResult<UserVo>> getUsers(@RequestBody UserDto userDto){
       PageResult<UserVo> users = tBannedService.getAllUsers(userDto);
        return Result.success(users);
    }

    /**
     * 封禁用户
     * @param banDto
     * @return 1为封禁成功，0为封禁失败
     */
    @PostMapping("/ban")
    public Result<Integer> ban(@RequestBody BanDto banDto){
        Integer ban = tBannedService.ban(banDto);
        return Result.success(ban);
    }

    /**
     * 解封用户
     * @param unBanDto
     * @return 0为解封成功，1为解封失败
     */
    @PostMapping("/unban")
    public Result<Integer> unban(@RequestBody UnBanDto unBanDto){
        Integer unban = tBannedService.unban(unBanDto);
        return Result.success(unban);
    }

    /**
     * 获取板块列表
     * @return
     */
    @GetMapping("/getAllSection")
    public Result<List<SectionVo>> getAllSection(){
        List<SectionVo> sectionVos = tBannedService.getAllSection();
        return Result.success(sectionVos);
    }

}
